import { PageConfig } from 'next';
import anylogger from 'anylogger';
import '@mmstudio/an000042';
import an48 from '@mmstudio/an000048';
import getuser from '../../../atoms/getuser';
import { tbcurriculums } from '../../../atoms/db';

const logger = anylogger('pages/api/teachgl/del.api');

export type Result = {
	ok: true;
} | {
	ok: false;
	message: string;
};

export type Message = {
	id: string;
}

/**
 * 删除课程
 */
const handler = an48<Result>();

handler.delete(async (req, res) => {
	try {
		logger.debug('msg body:', req.body);
		const user = await getuser(req);
		if (!user || user.role !== 'admin') {
			res.status(200).json({
				ok: false,
				message: '无权限'
			});
			return;
		}
		const { id } = req.body as Message;
		if (!id) {
			res.status(200).json({
				ok: false,
				message: 'ID 不存在'
			});
			return;
		}
		await tbcurriculums().del().where({ course_id: id });
		res.status(200).json({ ok: true });
	} catch (error) {
		logger.trace(error);
		res.status(200).json({ ok: false, message: (error as Error).message });
	}
});

export const config = {} as PageConfig;

export default handler;
